package com.ysw.sql;

import com.ysw.entity.Student;

import javax.jws.soap.SOAPBinding;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MyExecutor implements Executor{
    private MyConfig myConfig =new MyConfig();


    /**
     * 查询方法
     * @param statement
     * @param parameter
     * @param <T>
     * @return
     */
    @Override
    public <T> T show(String statement,Object parameter){
        System.out.println("--------开始执行查询方法----------");
        Connection connection=getConnection();
        ResultSet resultSet =null;
        PreparedStatement preparedStatement=null;
        try{
            preparedStatement=connection.prepareStatement(statement);
            //设置参数
            preparedStatement.setString(1,parameter.toString());
            resultSet=preparedStatement.executeQuery();
            Student student =new Student();
            System.out.println("-------查询开始遍历------");
            while (resultSet.next()){
                student.setId(resultSet.getInt(1));
                student.setStuname(resultSet.getString(2));
                student.setAge(resultSet.getInt(3));
                student.setSex(resultSet.getString(4));
                student.setStunumber(resultSet.getInt(5));
            }
            System.out.println("------查询遍历结束-------");
            return (T) student;
        } catch (SQLException e) {
            System.out.println("------查询遍历出错--------");
            e.printStackTrace();
        }finally {
            close(resultSet,preparedStatement,connection);
        }
        return null;
    }

    /**
     * 添加方法
     * @param statement
     * @param parameter
     */
    @Override
    public void save(String statement,Object parameter) {
        System.out.println("--------开始执行添加方法----------");
        Connection connection = getConnection();
        PreparedStatement preparedStatement = null;
        try{
            preparedStatement=connection.prepareStatement(statement);
            //设置参数
            String[] strings=parameter.toString().split(",");
//            preparedStatement.setInt(1,Integer.parseInt(strings[0]));
            preparedStatement.setString(1,strings[1]);
            preparedStatement.setString(2,strings[2]);
            preparedStatement.setString(3,strings[3]);
            preparedStatement.setString(4,strings[4]);
            preparedStatement.execute();
            commit(connection);
            System.out.println("--------添加参数设置成功--------");
        } catch (SQLException e) {
            rollback(connection);
            System.out.println("-------添加参数设置失败--------");
            e.printStackTrace();
        }finally {
            close(null,preparedStatement,connection);
        }

    }

    /**
     * 修改方法
     * @param statement
     * @param parameter
     */
    @Override
    public void mod(String statement,Object parameter) {
        System.out.println("--------开始执行修改方法----------");
        Connection connection = getConnection();
        PreparedStatement preparedStatement = null;
        try{
            preparedStatement=connection.prepareStatement(statement);
            //设置参数
            String[] strings=parameter.toString().split(",");
            preparedStatement.setString(5,strings[0]);
            preparedStatement.setString(1,strings[1]);
            preparedStatement.setString(2,strings[2]);
            preparedStatement.setString(3,strings[3]);
            preparedStatement.setString(4,strings[4]);
            preparedStatement.execute();
            commit(connection);
            System.out.println("--------修改参数更新成功--------");
        } catch (SQLException e) {
            rollback(connection);
            System.out.println("-------修改参数更新失败--------");
            e.printStackTrace();
        }finally {
            close(null,preparedStatement,connection);
        }

    }

    /**
     * 删除方法
     * @param statement
     * @param parameter
     */
    @Override
    public void del(String statement,Object parameter) {
        System.out.println("--------开始执行删除方法----------");
        Connection connection = getConnection();
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(statement);
            preparedStatement.setString(1,parameter.toString());
            preparedStatement.execute();
            commit(connection);
            System.out.println("--------删除参数设置成功--------");
        } catch (SQLException e) {
            rollback(connection);
            System.out.println("--------修改参数更新失败--------");
            e.printStackTrace();
        }finally {
            close(null,preparedStatement,connection);
        }
    }

    /**
     * 加载配置文件
     * 连接数据库
     * @return
     */
    private Connection getConnection() {
        try {
            Connection connection = myConfig.bulid();
            connection.setAutoCommit(false);
            System.out.println("~~~~~~~数据库连接成功~~~~~~~~");
            return connection;
        } catch (SQLException e) {
            System.out.println("~~~~~~~数据库连接成功~~~~~~~~");
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 关闭资源
     * @param resultSet
     * @param preparedStatement
     * @param connection
     */
    private void close(ResultSet resultSet,PreparedStatement preparedStatement,Connection connection){
        try{
            if(preparedStatement!=null){
                preparedStatement.close();
                System.out.println("preparedStatement已关闭!!!!!");
            }
            if(connection!=null){
                connection.close();
                System.out.println("connection已关闭!!!!!");
            }
        }catch(SQLException e){
            System.out.println("！！！！！！！关闭资源失败！！！！！！");
            e.printStackTrace();
        }
    }

    /**
     * 事物的提交
     * @param connection
     */
    private void commit(Connection connection){
        try{
            connection.commit();
        }catch (SQLException e){
            e.printStackTrace();
        }
    }

    /**
     * 事物的回滚
     * @param connection
     */
    private void rollback(Connection connection){
        try{
            connection.rollback();
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
}
